/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <QComboBox>
#include <QSettings>
#include <QString>
#include <QtGui>

#include <exception>
#include <iostream>
#include <list>

#include "mainwindow.h"
#include "controlbar.h"
#include "../constants.h"

/**
 * Constructor
 */

MainWindow::MainWindow(QApplication *app) {
  controlBar = new ControlBar(this);

  factory = new URLFactory();
  std::list<QString> *list = factory->getChannelList();

  std::list<QString>::iterator it;
  for(it = list->begin(); it != list->end(); it++) {
    controlBar->addItem(*it);
  }

  // And the teletekst viewer
  ttdisp = new TTDisp(this);

  // now the controls itself
  controller = new Controller(ttdisp, factory);

  // Initialise signal
  controlBar->connect(controller, app);

  controlBarWidget = controlBar->getWidget();
  controlBarWidget->setAllowedAreas(Qt::BottomDockWidgetArea);
  addDockWidget(Qt::BottomDockWidgetArea, controlBarWidget);

  setCentralWidget(ttdisp->getWidget());

  controller->updateTTDisp();
}

/**
 * Destructor
 */

MainWindow::~MainWindow() {
  delete controlBar;
  delete controller;
}

/**
 * closeEvent - Sent on x button for the window
 */

void MainWindow::closeEvent(QCloseEvent *event) {
  QString org = QString(AUTHOR);
  QString app = QString(BINARY);

  QSettings settings(org, app);
  settings.setValue("pos", pos());
  settings.setValue("size", size());

  event->accept();
}
